package com.finger.gulimall.product.service.impl;

import com.finger.gulimall.product.service.AttrService;
import com.finger.gulimall.product.vo.BaseAttrs;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.Query;

import com.finger.gulimall.product.dao.ProductAttrValueDao;
import com.finger.gulimall.product.entity.ProductAttrValueEntity;
import com.finger.gulimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Log4j2
@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveProductAttrValue(List<BaseAttrs> baseAttrs, Long spuId) {
        if (baseAttrs != null || baseAttrs.size() != 0) {
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attrVo -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                Long attrId = attrVo.getAttrId();
                productAttrValueEntity.setAttrId(attrId);
                productAttrValueEntity.setSpuId(spuId);
                // 上传不带属性名，之后页面检索会有问题，所以要加上属性名
                String attrName = attrService.getAttrNameByAttrId(attrId);
                productAttrValueEntity.setAttrName(attrName);
                log.info("attrName====：" + attrVo.getAttrName());
                productAttrValueEntity.setAttrValue(attrVo.getAttrValues());
                productAttrValueEntity.setQuickShow(attrVo.getShowDesc());
                return productAttrValueEntity;
            }).collect(Collectors.toList());

            this.saveBatch(collect);
        }

    }

    @Override
    public List<ProductAttrValueEntity> getListAttrForSpu(Long spuId) {

        QueryWrapper<ProductAttrValueEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id", spuId);
        // 查询该商品spu的全部属性
        List<ProductAttrValueEntity> productAttrValueEntities = baseMapper.selectList(queryWrapper);
        return productAttrValueEntities;
    }

    @Transactional
    @Override
    public void updateSpuAttrValues(Long spuId, List<ProductAttrValueEntity> entitys) {
        // 先删除该spu下的全部规格属性
        QueryWrapper<ProductAttrValueEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id", spuId);
        baseMapper.delete(queryWrapper);
        entitys.stream().forEach(item -> {
            item.setSpuId(spuId);
            item.setAttrSort(0);
        });
        // 再重新添加该商品的全部规格属性
        this.saveBatch(entitys);
    }

}